from typing import Optional
from pydantic import BaseModel
from models.user_models import Gender


class UserCreate(BaseModel):
    name:str
    age:int
    gender:Gender

    username:str
    password:str

    def check(self):
        """
        验证字段是否符合要求
        如果不符合则抛出ValueError
        """
        if not 0 < len(self.name) < 50:
            raise ValueError("姓名长度必须在1到50个字符之间")
        if not 0 <= self.age <= 150:
            raise ValueError("年龄必须在0到150岁之间")
        if not 0 < len(self.username) < 50:
            raise ValueError("用户名长度必须在1到50个字符之间")
        if not 0 < len(self.password) <= 60:
            raise ValueError("密码长度必须在1到60个字符之间")

class UserUpdate(BaseModel):
    name: Optional[str]=None
    age: Optional[int]=None
    gender: Optional[Gender]=None

    username: Optional[str]=None
    password: Optional[str]=None

    def check(self):
        """
        验证提供的字段是否符合要求
        如果不符合则抛出ValueError
        """
        if self.name is not None and not 0 < len(self.name) < 50:
            raise ValueError("姓名长度必须在1到50个字符之间")
        if self.age is not None and not 0 <= self.age <= 150:
            raise ValueError("年龄必须在0到150岁之间")
        if self.username is not None and not 0 < len(self.username) < 50:
            raise ValueError("用户名长度必须在1到50个字符之间")
        if self.password is not None and not 0 < len(self.password) <= 60:
            raise ValueError("密码长度必须在1到60个字符之间")
